<?php

/**
 * contact actions.
 *
 * @package    semramode
 * @subpackage contact
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class contactActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->contacts = Doctrine::getTable('contact')
      ->createQuery('a')
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {

  $this->getUser()->setAttribute('breadcrumb', '');
$breadcrumb = new breadcrumb();
$route[0]['title'] = 'Contact';
$route[0]['link'] = 'Contact';
$this->breadcrumb = $breadcrumb->create($this->getUser(), $route);
$this->getUser()->setAttribute('breadcrumb', $this->breadcrumb);
    $this->contacts = Doctrine::getTable('contact')->getContacts();
    $this->forward404Unless($this->contacts);
  }
  public function executeMap(sfWebRequest $request){
      $contact = Doctrine::getTable('contact')->findMap($request->getParameter('id'));
      return $this->renderPartial('map', array('map' => $contact[0]));
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new contactForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new contactForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($contact = Doctrine::getTable('contact')->find(array($request->getParameter('id'))), sprintf('Object contact does not exist (%s).', $request->getParameter('id')));
    $this->form = new contactForm($contact);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($contact = Doctrine::getTable('contact')->find(array($request->getParameter('id'))), sprintf('Object contact does not exist (%s).', $request->getParameter('id')));
    $this->form = new contactForm($contact);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($contact = Doctrine::getTable('contact')->find(array($request->getParameter('id'))), sprintf('Object contact does not exist (%s).', $request->getParameter('id')));
    $contact->delete();

    $this->redirect('contact/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $contact = $form->save();

      $this->redirect('contact/edit?id='.$contact->getId());
    }
  }
}
